<?php

define('DBCONFIG', 'C:\xampp\htdocs\GreenwichFreecycle\include\mysql.inc.php');


/*
 *function to check if username has already been assigned
 *
 */
function userNameAlreadyAssignDb($compareUser) {
//    $host = 'localhost';
//    $user = 'root';
//    $passwd = '';
//    $dbName = 'GreenwichFreecycleDb';
    
    // input connection details into scope
include DBCONFIG;//'C:\xampp\htdocs\GreenwichFreecycle\include\mysql.inc.php';
    $link = mysql_connect($host, $user, $passwd);
    if (!$link) {
        die('Could not connect: ' . mysql_error());// handle connection errors
    }
else {
    mysql_select_db($dbName, $link);

    // query if username is already entered
    $query = sprintf("SELECT username FROM fsuser WHERE username LIKE '%s'", mysql_real_escape_string($compareUser));
    $result = mysql_query($query);

    $row = mysql_fetch_assoc($result);}
    mysql_close($link);// close connection whether error or not
    return $row["username"];// return username if it is already in the database
    //, if not will a index not set error occur?

}

function passwordAlreadyAssignDb($comparePasswd) {
//    $host = 'localhost';
//    $user = 'root';
//    $passwd = '';
//    $dbName = 'GreenwichFreecycleDb';
    
    // input connection details into scope
include DBCONFIG;//'C:\xampp\htdocs\GreenwichFreecycle\include\mysql.inc.php';
    $link = mysql_connect($host, $user, $passwd);
    if (!$link) {
        die('Could not connect: ' . mysql_error());// handle connection errors
    }
else {
    mysql_select_db($dbName, $link);

    // query if username is already entered
    $query = sprintf("SELECT password FROM fsuser WHERE password LIKE '%s'", mysql_real_escape_string($comparePasswd));
    $result = mysql_query($query);

    $row = mysql_fetch_assoc($result);}
    mysql_close($link);// close connection whether error or not
    return $row["password"];// return username if it is already in the database
    //, if not will a index not set error occur?

}
/*
 *function found out if validation code has perviously been generated
 * returning true if it has
 *
 */
function isVCodeAlreadyStoredDb($compareCode) {
//    $host $user $passwd $dbName from mysql.inc.php
  
include DBCONFIG;//'C:\xampp\htdocs\GreenwichFreecycle\include\mysql.inc.php';
    $link = mysql_connect($host, $user, $passwd);
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
else {
    mysql_select_db($dbName, $link);

    $query = sprintf("SELECT activation_code FROM fsuser WHERE activation_code LIKE '%s'", mysql_real_escape_string($compareCode));
    $result = mysql_query($query);

    $row = mysql_fetch_assoc($result);
    mysql_close($link);
    return $row["activation_code"] === $compareCode;
}
}
/*
 * function to store activation of user account
 */
function activateAccountDb($userActivationCodeKey) {// have to change this to change
    
    //input connection details into scope
    include DBCONFIG;//'C:\xampp\htdocs\GreenwichFreecycle\include\mysql.inc.php';
    $link = mysql_connect($host, $user, $passwd);// use connection details
    if (!$link) {
        die('Could not connect: ' . mysql_error());// handle connection errors
    }
else {
    mysql_select_db($dbName, $link);//

    $query = sprintf("UPDATE fsuser SET activated = true WHERE activation_code = '%s'", mysql_real_escape_string($userActivationCodeKey));
    $result = mysql_query($query);

    $row = mysql_affected_rows();}// return the number of effect rows, should be one, as integer
    mysql_close($link);// close connection whether error or not
    return $row;// return number of effected rows

}

/**
 * function to store user details at account creation but not yet verified
 */
function setupAccountDetailsDb($userName, $userEmail, $userPassword, $userPostcode)
{
    // import connection details into scope
    include DBCONFIG;//'C:\xampp\htdocs\GreenwichFreecycle\include\mysql.inc.php';
    $link = mysql_connect($host, $user, $passwd);// use connection details to setup link
       if  (!$link){
        die('Could not connect: ' . mysql_error());// handle sql connection error
       }
   else {
       // insert user details into database
      mysql_select_db($dbName);
      $query = sprintf("INSERT INTO FSuser (username, password, email_address, postcode)" . 
              "VALUES ('%s', '%s', '%s', '%s')" ,
              mysql_real_escape_string($userName),
              mysql_real_escape_string($userEmail),
              sha1(mysql_real_escape_string($userPassword)),// encrypted password before saving
              mysql_real_escape_string($userPostcode)
              );
      if ( !mysql_query($query,$link) ) {
         die('Insert error: ' . mysql_error());// handle error during insert
         mysql_close($link);     //close connection if errors
         return FALSE;
      } else {
         mysql_close($link);// close connection if no errors
         return TRUE;
      }
      
   }
}



//function dbEscapeString()
//{
//    
//}
//?>
